#include "../include/menu.h"
#include "../include/administrator.h"
#include "../include/passenger.h"
#include "../include/trips.h"
#include "../include/ticket.h"
#include "../include/longin.h"


int main(int argc,const char* argv[])
{       
    //日志加载
    initLogFile();
    //一级菜单控制变量
    int num = 1;
    //管理员中所有变量//
    //管理员头节点
    ADM *admhead = NULL;
    admhead = admHead(admhead);
    //默认管理员
    admhead = defaultAdm(admhead);
    //管理员级菜单控制变量
    int admnum = 1;
    //当前登录的管理员
    ADM *admkey = NULL;
    //车次中所有变量//
    //车次头节点创建
    TRAIN *triphead = NULL;
    triphead = tripsHead(triphead);
    //车次级菜单控制变量
    int tripnum = 1;

    //乘客中所有变量//
    //乘客头节点
    PASS *pashead = NULL;
    pashead = passengerHead(pashead);
    //乘客登录级控制变量
    int pasnum = 1;
    //乘客购票级控制变量
    int ticketnum = 1;
    //当前登录乘客
    PASS *paskey = NULL;

    //乘客数据
    FILE *pfpas = NULL;
    pfpas = fopen("../doc/passenger.txt", "rb");
    if(pfpas == NULL)
    {
        perror("fopen passenger");
        pfpas = fopen("../doc/passenger.txt", "wb");
        fclose(pfpas);
        pfpas = fopen("../doc/passenger.txt", "rb");
    }

    PASS *pasend = pashead;
    PASS *newpas = pashead;
    PASSENGER pastmp;
    while(fread(&(pastmp), sizeof(PASSENGER), 1, pfpas) == 1)
    {
	    newpas = (PASS *)malloc(sizeof(PASS));  
        newpas->pas = pastmp;    
        pasend->next = newpas;  
        pasend = newpas;  
    }
    newpas->next = NULL;
    fclose(pfpas);

    //管理员数据
    FILE *pfadm = NULL;
    pfadm = fopen("../doc/administrator.txt", "rb");
    if(pfadm == NULL)
    {
        perror("fopen administrator");
        pfadm = fopen("../doc/administrator.txt", "wb");
        fclose(pfadm);
        pfadm = fopen("../doc/administrator.txt", "rb");
    }

    ADM *admend = admhead;
    ADM *newadm = admhead;
    ADMIN admtmp;
    while(fread(&(admtmp), sizeof(ADMIN), 1, pfadm) == 1)
    {
	    newadm = (ADM *)malloc(sizeof(ADM));  
        newadm->adm = admtmp;  
        admend->next = newadm;  
        admend = newadm;  
    }
    newadm->next = NULL; 
    fclose(pfadm);

    //车次数据
    FILE *pftrip = NULL;
    pftrip = fopen("../doc/trips.txt", "rb");
    if(pftrip == NULL)
    {
        perror("fopen trips");
        pftrip = fopen("../doc/trips.txt", "wb");
        fclose(pftrip);
        pftrip = fopen("../doc/trips.txt", "rb");
    }

    TRAIN *tripend = triphead;
    TRAIN *newtrips = triphead;
    TRAINS tmp;
    while(fread(&(tmp), sizeof(TRAINS), 1, pftrip) == 1)
    {
	    newtrips = (TRAIN *)malloc(sizeof(TRAIN)); 
        newtrips->tripsan = tmp;    
        tripend->next = newtrips;   
        tripend = newtrips; 
    }
    newtrips->next = NULL;
    fclose(pftrip);

    int count = 0;
    num = 1;
    while(num)
    {
        //打开主菜单
        menu();
        num = 1;
        printf("请选择：");
        count = scanf("%d", &num);
        if(count == 0)
        {
            getchar();
            printf("输入有误！\n");
            printf("请重新选择！\n");
            continue;
        }
        switch(num)
        {
            case 0: num = 0;
                    break;
            case 1: admnum = 1;
                    while(admnum)
                    {
                        //打开管理员登录菜单
                        administratorMenu();
                        admnum = 1;
                        printf("请选择：");
                        count = scanf("%d", &admnum);
                        if(count == 0)
                        {
                            getchar();
                            printf("输入有误！\n");
                            printf("请重新选择！\n");
                            continue;
                        }
                        switch(admnum)
                        {
                            case 0: admnum = 0;
                                    break;
                            case 1: //管理员登录
                                    admkey = admLogin(admhead);
                                    if(admkey != NULL)
                                    {
                                        tripnum = 1;
                                        while(tripnum)
                                        {
                                            //打开管理员操作菜单
                                            administratorMenu_login();
                                            tripnum = 1;
                                            printf("请选择：");
                                            count = scanf("%d", &tripnum);
                                            if(count == 0)
                                            {
                                                getchar();
                                                printf("输入有误！\n");
                                                printf("请重新选择！\n");
                                                continue;
                                            }
                                            switch(tripnum)
                                            {
                                                case 0: tripnum = 0;
                                                        break;
                                                case 1://查看
                                                        print(triphead);
                                                        break;
                                                case 2://增加
                                                        triphead = addtrips(triphead, admkey);
                                                        break;
                                                case 3://停运
                                                        triphead = stopTrips(triphead, admkey);
                                                        break;
                                                case 4://修改
                                                        triphead = changeTrips(triphead, admkey);
                                                        break;
                                                case 5://改密码
                                                        admhead = admPwd(admhead);
                                                        tripnum = 0;
                                                        printf("请重新登录！\n");
                                                        break;
                                                case 6://个人信息
                                                        admprint(admkey);
                                                        break;
                                                default:
                                                    printf("没有这个选项！\n");
                                                    break;
                                            }
                                        }
                                    }
                                    break;
                            case 2: //增加管理员
                                    admhead = newAdm(admhead);
                                    break;
                            default:
                                printf("没有这个选项！\n");
                                break;
                        }
                    }
                    break;
            case 2: pasnum = 1;
                    while(pasnum)
                    {
                        //打开乘客登录菜单
                        tripsmenu_longin();
                        pasnum = 1;
                        printf("请选择：");
                        count = scanf("%d", &pasnum);
                        if(count == 0)
                        {
                            getchar();
                            printf("输入有误！\n");
                            printf("请重新选择！\n");
                            continue;
                        }
                        switch(pasnum)
                        {
                            case 0: pasnum = 0;
                                    break;
                            case 1: //登录
                                    paskey = passengerLogin(pashead);
                                    if(paskey != NULL)
                                    {
                                        ticketnum = 1;
                                        while(ticketnum)
                                        {
                                            //打开乘客操作界面
                                            tripsmenu();
                                            ticketnum = 1;
                                            printf("请选择：");
                                            count = scanf("%d", &ticketnum);
                                            if(count == 0)
                                            {
                                                getchar();
                                                printf("输入有误！\n");
                                                printf("请重新选择！\n");
                                                continue;
                                            }
                                            switch(ticketnum)
                                            {
                                                case 0: ticketnum = 0;
                                                        break;
                                                case 1://查询
                                                        findTrips(triphead);
                                                        logMessage(LOG_INFO, "findTrips %s", paskey->pas.name);
                                                        break;
                                                case 2://购买
                                                        print(triphead);
                                                        buyTicket(triphead, pashead);
                                                        break;
                                                case 3://改签
                                                        changeTicket(triphead, pashead);
                                                        break;
                                                case 4://退票
                                                        lossTicket(triphead, pashead); 
                                                        break;
                                                case 5://个人信息
                                                        output(paskey);
                                                        break;
                                                case 6://改密码
                                                        pashead = passengerPwd(pashead);
                                                        ticketnum = 0;
                                                        printf("请重新登录！\n");
                                                        break;
                                                default:
                                                    printf("没有这个选项！\n");
                                                    break;
                                            }
                                        }
                                    }
                                    break;
                            case 2: //注册
                                    pashead = newPassenger(pashead);
                                    break;
                            default:
                                printf("没有这个选项！\n");
                                break;   
                        }
                    }
                    break;
            default:
                printf("没有这个选项！\n");
                break;
        }
    }

    //数据写入
    //乘客数据
    pfpas = fopen("../doc/passenger.txt", "wb");
    if(pfpas == NULL)
    {
        perror("fopen passenger");
        exit(1);
    }

    pasend = pashead->next;
    while(pasend != NULL)
    {
        fwrite(&(pasend->pas), sizeof(PASSENGER), 1, pfpas);
        pasend = pasend->next;
    }
    fclose(pfpas);

    //管理员数据
    pfadm = fopen("../doc/administrator.txt", "wb");
    if(pfadm == NULL)
    {
        perror("fopen administrator");
        exit(1);
    }

    admend = admhead->next;
    while(admend != NULL)
    {
        fwrite(&(admend->adm), sizeof(ADMIN), 1, pfadm);
        admend = admend->next;
    }
    fclose(pfadm);

    //车次数据
    pftrip = fopen("../doc/trips.txt", "wb");
    if(pftrip == NULL)
    {
        perror("fopen trips");
        exit(1);
    }

    tripend = triphead->next;
    while(tripend != NULL)
    {
        fwrite(&(tripend->tripsan), sizeof(TRAINS), 1, pftrip);
        tripend = tripend->next;
    }
    fclose(pftrip);

    //关闭日志文件
    closeLogFile();

    //释放空间
    freeadm(admhead);
    freepas(pashead);
    freetrips(triphead);

    return 0;
}
